//
//  BPInvitationViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/2/6.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPInvitationViewController.h"
#import "BPInvitationTableViewCell.h"

@interface BPInvitationViewController () <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *wechatBtn;
@property (weak, nonatomic) IBOutlet UIButton *friendBtn;
@property (weak, nonatomic) IBOutlet UIButton *weiboBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *detailLbl;

@property (weak, nonatomic) IBOutlet UILabel *moneyTotal;
@property (weak, nonatomic) IBOutlet UILabel *peopleTotal;

@property (strong, nonatomic) NSDictionary *data;

@end

@implementation BPInvitationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"邀请有奖";
    [self getData];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"BPInvitationTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.detailLbl.userInteractionEnabled = YES;
    [self.detailLbl addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushDetailVC)]];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}

- (void)getData {
    [SVProgressHUD showWithStatus:@"读取中..."];
    NSDictionary *user = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    
    [mgr POST:[NSString stringWithFormat:@"%@?c=User&&a=inviteReward",requestURL]
   parameters:@{@"token" : user[@"token"]}
     progress:^(NSProgress * _Nonnull uploadProgress) {
         
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
             [SVProgressHUD dismiss];
             NSLog(@"邀请有奖 %@",responseObject);
             self.data = responseObject[@"data"];
             long money_total = [self.data[@"money_total"] longValue];
             long people_total = [self.data[@"people_total"] longValue];
             self.moneyTotal.text = [NSString stringWithFormat:@"%li", money_total];
             self.peopleTotal.text = [NSString stringWithFormat:@"%li", people_total];
             [self.tableView reloadData];
         }
         else {
             [SVProgressHUD showInfoWithStatus:@"请求错误"];
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [SVProgressHUD dismiss];
             });
         }
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         [SVProgressHUD showInfoWithStatus:@"网络故障"];
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             [SVProgressHUD dismiss];
         });
     }];
}

- (void)pushDetailVC {
    UIViewController *vc = [[UIViewController alloc]init];
    vc.title = @"奖励细则";
    vc.view.backgroundColor = [UIColor colorWithRed:0xec / 255.0 green:0xf5 / 255.0 blue:0xfc / 255.0 alpha:1.0];
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(16, 0, screenW - 16 * 2, screenH - 84);
    label.numberOfLines = 0;
    label.text = @"1.  分享链接至您的好友或朋友圈，若该用户首次注册使用即可获得30元无门槛抵用券，优惠券长期有效；\n2.  该用户在首次成功订购拍摄套餐后，您将获得5元现金红包至您的余额账户中，可用于提现；\n3.  5元现金红包将在您分享好友成功完成首单且无退款的5天后到达您的账户，详见【累计收益】；\n4.  拥有相同手机号、支付账号、贝贝摄影账号与硬件设备的用户，均视为同一用户，仅对新用户成功首单且无退单进行返现；\n5.  如发现任何违规套取奖励行为将视情节严重程度进行判罚：不予发放奖励、停封推荐有奖功能、冻结所有通过推荐有奖渠道获得的奖励、依法追究其法律责任；\n6.  如有其它疑问请咨询贝贝摄影官方客服";
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    label.textColor = [UIColor colorWithRed:74/255.0 green:144/255.0 blue:226/255.0 alpha:1/1.0];
    [vc.view addSubview:label];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 23;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BPInvitationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.numberLbl.text = self.data[@"list"][indexPath.row][@"phone"];
    cell.statusLbl.text = self.data[@"list"][indexPath.row][@"statusDsc"];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *list = self.data[@"list"];
    return list.count;
}

@end
